include_javaScript(url_prefix + 'editor/ckeditor/ckeditor.js');
include_javaScript(url_prefix + 'editor/ckfinder/ckfinder.js');


$(function () {
    $("#tabs").tabs();
});

$(document).ready(function () {

    hs.graphicsDir = url_base + 'app/admin/view/temp/default/layout/css/highslide/graphics/';
    hs.align = 'center';
    hs.closeText = '<img border="0" src="' + url_base + 'app/admin/view/temp/default/layout/css/highslide/graphics/close_X.png" />';
    hs.creditsText = 'this.a.title';
    hs.outlineType = 'rounded-white';
    hs.showCredits = false;
    hs.wrapperClassName = 'draggable-header';
    hs.cacheAjax = false;
    hs.dimmingOpacity = 0.7;
    hs.onDimmerClick = function () {
        return false;
    }


    $('#btn-submit').click(function () {

        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var emailaddressVal = $("#Email").val();
        if (emailaddressVal == '') {
            $(".error").show().text(_['error-emal-empty']);
            hasError = true;
        }

        else if (!emailReg.test(emailaddressVal)) {
            $(".error").show().text(_['error-emal-no-format']);
            hasError = true;
        }

        if (hasError == true) {
            return false;
        }

    });

    $('input:checkbox[name="delId[]"]').click(function () {

        if ($(this).attr("checked") == "checked") {

            $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled", false);

        } else {

            if (getValCb().length > 0) {
                $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled", false);
            } else {
                $('.but_tool').removeClass('but_login').addClass('butx').attr("disabled", true);
            }


        }


    });
});

function checkAll() {
    if (getValCb().length > 0) {

        $('.but_tool').removeClass('but_login').addClass('butx').attr("disabled", true);

        $('input:checkbox[name="all"]').removeAttr("checked");
        $('input:checkbox[name="delId[]"]').removeAttr("checked");

    } else {

        $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled", false);

        $('input:checkbox[name="all"]').attr("checked", "checked");
        $('input:checkbox[name="delId[]"]').attr("checked", "checked");
    }

}

function getValCb() {
    var delId = [];
    $('input:checkbox[name="delId[]"]:checked')
        .each(function (index) {
            delId.push($(this).val());
        });

    return delId;
}

function del_pic(path_pic, CatID) {
    var url = _seokey[0] + "/" + _seokey[1] + "/ajax/delpic/" + CatID + "/" + path_pic + "/";
    $.ajax({
        type:"GET",
        url:url_base + url,
        cache:false,
        dataType:"html",
        success:function (DataHtml) {
            document.getElementById('id_img').src = url_base + 'upload/noimg.png';
            $('#id_img_but').hide();
            $('#id_img_input').show();
        }

    });

}

function processChecked(active ,StrID) {
    if(StrID=="")
    var StrID = getValCb();


    switch (active) {
        case 'notview':
            var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/" + active + "/" + StrID;
            var sms = _['showx'];
            var smsError = _['error_show'];
            var errorNotOK = _['error_ok_show'];
            var status = _['notview'];
            break;

        case 'process':
            var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/" + active + "/" + StrID;
            var sms = _['showx'];
            var smsError = _['error_show'];
            var errorNotOK = _['error_ok_show'];
            var status = _['process'];
            break;

        case 'finished':
            var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/" + active + "/" + StrID;
            var sms = _['hidex'];
            var smsError = _['error_hide'];
            var errorNotOK = _['error_ok_hide'];
            var status = _['finished'];
            break;

        case 'cancel':
            var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/" + active + "/" + StrID;
            var sms = _['delx'];
            var smsError = _['error_del'];
            var errorNotOK = _['error_ok_del'];
            var status = _['cancel'];
            break;

        case 'checkout':
            var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/" + active + "/" + StrID;
            var sms = _['delx'];
            var smsError = _['error_del'];
            var errorNotOK = _['error_ok_del'];
            var status = _['checkout'];
            break;

        case 'delete':
            var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/" + active + "/" + StrID;
            var sms = _['delete'];
            var smsError = _['error_delete'];
            var errorNotOK = _['error_ok_delete'];
            var deletex = 'ok';
            break;

    }

    if (StrID != "") {
        $.ajax({
            type:"POST",
            url:url_base + "" + strData,
            cache:false,
            dataType:"html",
            success:function (DataHtml) {

                if (DataHtml != 'noOk' && active != "delete"&& StrID!="") {
                         var rs =JSON.parse(DataHtml);
                        $('input:checkbox[name="delId[]"]:checked')
                            .each(function (index) {
                               $(".status" + $(this).val()).html(status);
                                if(parseInt(rs[$(this).val()])>0)
                                {
                                    $("#btn_sent"+$(this).val()).val(_['sent_ok']+"("+rs[$(this).val()]+")");
                                }
                                else
                                {
                                    $("#btn_sent"+$(this).val()).val(_['sent_no']);
                                }
                                var email = $("#email"+$(this).val()).attr("title");
                                $("#btn_sent"+$(this).val()).attr("onclick", "sentmail('" +email+"','"+active+"','"+$(this).val()+"')");
                                $("#amount"+$(this).val()).val(parseInt(rs[$(this).val()]));
                            });

                }
                else if(DataHtml == 'ok' && active == "delete")
                {
                    window.location =  _seokey[0] + "/" + _seokey[1];
                }


                else if (DataHtml != "ok") {
                    alert(errorNotOK);
                }
            }
        });
    } else {
        alert(smsError);
    }
}


function sentmail(email,status,ID) {
    var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/mail/" + email+"/"+status+"/"+ID;
    var amount = $("#amount"+ID).val();
    $("#btn_"+ID).attr("style","background:center url('"+url_base+"upload/ajax-loader.gif') no-repeat");
    var btn =$("#btn_"+ID).html();
    $("#btn_"+ID).html("");
    $.ajax({
        type:"POST",
        url:url_base + "" + strData,
        cache:false,
        dataType:"html",
        success:function (DataHtml) {
            if(DataHtml == "ok")
            {
               $("#btn_"+ID).attr("style","background:center url('"+url_base+"upload/ajax-loader.gif') no-repeat");
                $("#btn_"+ID).html(btn);
                amount = parseInt(amount) + 1 ;
                $("#amount"+ID).val(amount);
                $("#btn_sent"+ID).val(_['sent_ok']+"("+amount+")")
            }

        }
    });
}


function create_seo(lang) {
    var $name = $('#Name_' + lang).val();
    var FullContents = 'FullContents_' + lang;
    var $seokey = getLinkSeo($name);

    $('#SeoKey_' + lang).val($seokey);
    $('#Title_' + lang).val($name);

    $('#Keyword_' + lang).val($name);
    $('#TitleH1_' + lang).val($name);


    var Desc = 'Desc_' + lang;
    var DescF = 'DescF_' + lang;
    var TitleH2 = 'TitleH2_' + lang;
    $('#TitleH2_' + lang).val($name);
    $('#Desc_' + lang).val($name);
    CKEDITOR.instances[DescF].setData($name);

    /*var $des = CKEDITOR.instances[FullContents].getData();
     CKEDITOR.instances[TitleH2].setData($name);
     CKEDITOR.instances[Desc].setData($name);
     */

}




